<?php
error_reporting(-1);

$URI = $_REQUEST['URI'];
$image = $_REQUEST['image'];

$file = "photo.html?URI=" . urlencode($URI);

if (!empty($URI)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.urlencode(basename($image)));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . getFileSize());
    ob_clean();
    flush();
    readfile("http://" . $_SERVER['SERVER_NAME'] . "/webapps/iphotos/" . $file);
    unlink("temp/temp.jpg");
    exit;
}

function getFileSize() {
	global $file;
	$content = file_get_contents("http://" . $_SERVER['SERVER_NAME'] . "/webapps/iphotos/" . $file);
	$handle = fopen("temp/temp.jpg", "w+");
	fwrite($handle, $content);
	fclose($handle);
	return filesize('temp/temp.jpg');
}

?>